package com.app.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
@Slf4j
public class SMSUtils {
    @Value("${aliyun.sms.accessKeyId}")
    private String ALIYUNACCESSKEYID;
    @Value("${aliyun.sms.secret}")
    private String ACCESSKEYSECRET;
    @Value("${aliyun.sms.notice.templateCode}")
    private String NOTICE;
    @Value("${aliyun.sms.signName}")
    private String SIGNNAME;

    /**
     * 报灾短信
     *
     * @param phoneNumbers 电话号码
     */
    public void noticeSms(String phoneNumbers, String type, String address, String phone) {
        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId("LTAI5tAYn23DQv3ryAzJawM8")
                    .accessKeySecret("sqavB651zqfMMLSDA5etENEKJINNW6")
                    .build());
            AsyncClient client = AsyncClient.builder()
                    .region("cn-shenzhen")
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                    .setEndpointOverride("dysmsapi.aliyuncs.com")
                    )
                    .build();
            Map<String, Object> request = new HashMap<>();
            String trimType = type.trim();
            String trimAddress = address.trim();
            String trimPhone = phone.trim();
            request.put("type", trimType);
            request.put("address", trimAddress);
            request.put("phone", trimPhone);
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName("肃北蒙古族自治县农业农村")
                    .phoneNumbers(phoneNumbers)
                    .templateCode("SMS_464776336")
                    .templateParam(JSONObject.toJSONString(request))
                    .build();
            log.info(JSONObject.toJSONString(sendSmsRequest));
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get();
            log.info(new Gson().toJson(resp));
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 动物救助短信
     *
     * @param phoneNumbers 电话号码
     */
    public void animalRescueSms(String phoneNumbers, String type, String address, String phone) {
        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId("LTAI5tAYn23DQv3ryAzJawM8")
                    .accessKeySecret("sqavB651zqfMMLSDA5etENEKJINNW6")
                    .build());
            AsyncClient client = AsyncClient.builder()
                    .region("cn-shenzhen")
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                    .setEndpointOverride("dysmsapi.aliyuncs.com")
                    )
                    .build();
            Map<String, Object> request = new HashMap<>();
            String trimType = type.trim();
            String trimAddress = address.trim();
            String trimPhone = phone.trim();
            request.put("type", trimType);
            request.put("address", trimAddress);
            request.put("phone", trimPhone);
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName("肃北蒙古族自治县农业农村")
                    .phoneNumbers(phoneNumbers)
                    .templateCode("SMS_465409689")
                    .templateParam(JSONObject.toJSONString(request))
                    .build();
            log.info(JSONObject.toJSONString(sendSmsRequest));
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get();
            log.info(new Gson().toJson(resp));
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 分配短信
     *
     * @param phoneNumbers 电话号码
     */
    public void allocationSms(String phoneNumbers,String name) {
        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId("LTAI5tAYn23DQv3ryAzJawM8")
                    .accessKeySecret("sqavB651zqfMMLSDA5etENEKJINNW6")
                    .build());
            AsyncClient client = AsyncClient.builder()
                    .region("cn-shenzhen")
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                    .setEndpointOverride("dysmsapi.aliyuncs.com")
                    )
                    .build();
            Map<String, Object> request = new HashMap<>();
            String trimName = name.trim();
            request.put("name", trimName);
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName("肃北蒙古族自治县农业农村")
                    .phoneNumbers(phoneNumbers)
                    .templateCode("SMS_465433436")
                    .templateParam(JSONObject.toJSONString(request))
                    .build();
            log.info(JSONObject.toJSONString(sendSmsRequest));
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get();
            log.info(new Gson().toJson(resp));
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
